package locedit
{
    import com.bit101.components.List;
    import com.bit101.components.PushButton;
    import com.bit101.components.RadioButton;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.StyleSheet;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import mge.Localization;
    import mge.ui.ModalDialog;
    import mge.ui.TextTyped;
    
	/**
	 * ...
	 * @author ivan
	 */
	public class Locedit extends Sprite 
	{
    private var idx:int = 0;
    private var loc:Localization;
    
    [Embed(source='../res/localization.xml', mimeType = "application/octet-stream")]
    public static const rLockit:Class;
    
    [Embed(source='../res/style.css', mimeType = "application/octet-stream")]
    public static const rStyle:Class;
        
		public function Locedit():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
        
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
            
      // Setup stage
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      
      // Draw a background
      var sprite:Sprite = new Sprite();
      sprite.graphics.beginFill(0xAAAAAA);
      sprite.graphics.drawRect(0, 0, 800, 600);
      sprite.graphics.endFill();
      addChild(sprite);
      
      loc = new Localization(new XML(new rLockit), "en");
      /*
      var textField:TextField = new TextField();
      textField.autoSize = TextFieldAutoSize.LEFT;
      addChild(textField);
      var textTyped:TextTyped = new TextTyped(textField);
      textTyped.type(loc.translate("DIALOG_ID_EXAMPLE").text);
      loc.language = "en";*/
      
      radioEn = new RadioButton(this, 50,  10, "en",  true, onLanguageChange);
      radioEs = new RadioButton(this, 100, 10, "es", false, onLanguageChange);
          
       // Textfield.
      textField = new TextField();
      textField.x = 250;
      textField.y = 100;
      textField.multiline = true;
      textField.autoSize = TextFieldAutoSize.LEFT;
      addChild(textField);
      style.parseCSS(new rStyle);
      textField.styleSheet = style;
      textField.backgroundColor = 0xFFFFFF;
      textField.background = true;
      
      var btn1:PushButton = new PushButton(this, 150, 550 , "Modal", onModal);
      var btn2:PushButton = new PushButton(this, 50, 550 , "Reload", onReload);
      btn1.width = btn2.width = 50;
      
      var array:Array = new Array();
      for (var i:int = 0; i < loc.tags.length; i++)
        array.push(loc.tags[i]);
      
      list = new List(this, 30, 30, array);
      list.height = 500;
      list.width = 200;
      
      list.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
      
      addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    
    private function onEnterFrame(e:Event):void
    {
      if (list.selectedIndex >= 0 && list.selectedIndex < loc.tags.length)
        textField.htmlText = loc.translate(loc.tags[list.selectedIndex]).text;
    }
    
    private function onMouseMove(e:Event):void
    {
      var over:int = list.mouseY / list.listItemHeight;
      if (over >= 0 && over < loc.tags.length)
        list.selectedIndex = over;
    }
        
    private function onModal(e:Event):void
    {
      if (list.selectedIndex >= 0 && list.selectedIndex < loc.tags.length)
        new ModalDialog(this, loc.translate(loc.tags[list.selectedIndex]).text, style);
    }
    
    private function onReload(e:Event):void
    {
      
    }
    
    private function onLanguageChange(e:Event):void
    {
      loc.language = (e.target as RadioButton).label;
    }
    
    private var textField:TextField;
    private var list:List;
    private var style:StyleSheet = new StyleSheet();
    private var radioEn:RadioButton;
    private var radioEs:RadioButton;
		
	}
	
}